/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import * as fse from 'fs-extra';
import path from 'path';

import { CommonConst } from '../../const/common-const.js';
import { TaskNames } from '../common/task-names.js';
import { TargetTaskService } from '../service/target-task-service.js';
import { PackageJsonOpt } from '../task/ohos-har-task.js';

import { CoreProcessPackageJson } from './core-process-package-json.js';
import { ArtifactType } from './har-extend-info.js';
import Task = TaskNames.Task;

/**
 * 闭源Har打包发布前需要对package.json文件进行处理和增加一些信息
 * 当前该任务执行处理很快,先不需要做增量
 */
export class ProcessPackageJson extends CoreProcessPackageJson {
  private readonly generatePackageJsonFilePath: string;

  constructor(taskService: TargetTaskService) {
    super(taskService, Task.PROCESS_PACKAGE_JSON);
    this.generatePackageJsonFilePath = path.resolve(this.generatePmDir, CommonConst.PACKAGE_JSON);
  }

  protected doTaskAction(): void {
    super.doTaskAction();

    fse.outputJSONSync(this.generatePackageJsonFilePath, this.packageJsonObj);
  }

  protected addArtifactType(packageJsonObj: PackageJsonOpt) {
    packageJsonObj.ohos.artifactType = ArtifactType.OBFUSCATION;
  }
}
